#for special explorer not global
import os,sys
import re
from  Utils.config import VIR_DIR
def _DIR(path):
    return os.path.abspath(path).replace('\\','/')

ACCESSMODE = {
            '0': "---"
            , '1': "--x"
            , '2': "-w-"
            , '3': "-wx"
            , '4': "r--"
            , '5': "r-x"
            , '6': "rw-"
            , '7': "rwx"
        }
def path_info_muti(infolist,tti):
    ret = {}
    if(len(infolist)==1 and infolist[0]['type']=="file"):
        state,ret = filedict(infolist[0]['path'])
        ret.update({'path':infolist[0]['path']})
    elif(len(infolist)==1 and infolist[0]['type']=="folder"):
        state,ret = filedict(infolist[0]['path'])
        ret.update({'path':infolist[0]['path']})
    else:
        father_name = os.path.basename(os.path.basename(infolist[0]['path']))
        ret.update(
            {
                'fileCount': 0
                , 'folderCount': 0
                , 'size':0
                , 'father_name': father_name
                , 'mod': ''
                , 'mode': 'not set'
                ,"path":os.path.dirname(infolist[0]['path'])
            })
        for f in infolist:
            state,fo = filedict(f['path'])

            if not state:
                ret['fileCount'] += 1
            else:
                ret['folderCount']+=1
            ret['size']+=fo['size']
    return ret
#目前用户获取 User和Public的目录表

def VTDirList_Fav():
    return []
def VTDirList(path):#realpath
    ret = []
    bd = path
    for d in os.listdir(path):
        isDir,di = filedict(bd+'/'+d)
        if isDir:
            ret.append(di)
    return ret

def _DIR_OUT(path):
    return  os.path.abspath(path).replace("\\",'/')

#返回文件或单个目录的信息
def filedict(file):
    pra = os.stat(file)
    prg = oct(os.stat(file).st_mode)[-3:]
    _file = {
        "name": os.path.basename(file),
        "path": file.replace('\\','/'),
        "mode": ACCESSMODE[prg[0]] + ACCESSMODE[prg[1]] + ACCESSMODE[prg[2]] + " (" + prg + ")",
        "atime": (int)(pra.st_atime),
        "ctime": (int)(pra.st_ctime),
        "mtime": (int)(pra.st_mtime),
        "isReadable": int(prg[0]) in [4, 5, 6, 7],
        "isWriteable": int(prg[0]) in [2, 3, 6, 7],

    }
    if os.path.isdir(file):
        pj = os.listdir(file)
        _file.update(
            {
                "type": "folder"
                , "isParent": pj.__len__()!=0
                ,"size":0
            }
        )
        return True, _file
    else:
        _file.update(
            {
                "type": "file"
                , "size": os.path.getsize(file)
                , "isParent": False
                , "ext": os.path.splitext(file)[-1][1:]
            }
        )
        return False, _file
def pathlist(self,path):
    test = path
    req_path = os.path.dirname(path)
    req_name = os.path.basename(path)

    ret = re.match("\{(.+)\}(\:[0-9]*)?(.+)", path.replace('\\','/'))
    DIR_NAME = ""
    pathType = ""
    GID = ""
    if ret is not None:
        v_dir = ret.group(1)
        pathType = "{" + v_dir + "}"
        base = VIR_DIR[v_dir]
        path = base + ret.group(3)
        GID = ret.group(2)
        if GID:
            DIR_NAME = "public"
    path = os.path.abspath(os.path.dirname(path) + '\\' + os.path.basename(path))
    files = os.listdir(path)

    folderList = []
    fileList = []
    for i in files:
        isD, fdic = self.filedict(path + '\\' + i)
        if isD:
            folderList.append(fdic)
        else:
            fileList.append(fdic)

    data = {
        "folderList": folderList
        , "fileList": fileList
        , "info": {
            "pathType": pathType,
            "role": "",
            "id": "" if not GID else GID.split(':')[-1],
            "name": DIR_NAME
        },
        "pathReadWrite": "writeable",
        "thisPath": req_path.replace('\\','/'),
        "userSpace": {
            "sizeMax": "0",
            "sizeUse": 1024
        }
        , "test": test
    }
    return data